package com.fruit.controller;

import com.fruit.ProductClient;
import com.fruit.ProductInfoOutPut;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @Description: RestTemplate进行通讯
 * @Author: LJ
 * @Date: 2018/10/25 11:44
 * @version:
 */
@RestController
@Slf4j
@RequestMapping("/productCategory")
public class ProductCategoryController {
    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private ProductClient productClient;

    /**
     * 第一种形式 通过RestTemplate调用获取商品服务(直接使用url写死)
     *
     * @return
     */
    @GetMapping("/getProductList1")
    public String getProductList1() {
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject("http://192.168.1.200:8080/product/list", String.class);
        log.info("response={}", response);
        return response;
    }

    /**
     * 第二种形式 负载均衡形式(启动多个服务情况) 利用loadBalancerClien通过应用名获取
     *
     * @return
     */
    @GetMapping("/getProductList2")
    public String getProductList2() {
        RestTemplate restTemplate = new RestTemplate();
        //返回实例(通过应用的名字获取IP端口)
        ServiceInstance serviceInstance = loadBalancerClient.choose("PRODUCT");
        //IP
        log.info("host={}", serviceInstance.getHost());
        //端口
        log.info("port={}", serviceInstance.getPort());
        //获取IP+端口 拼接 调用RestTemplate
        String url = String.format("http://%s:%s", serviceInstance.getHost(), serviceInstance.getPort()) + "/product/list";
        String response = restTemplate.getForObject(url, String.class);
        log.info("response={}", response);
        return response;
    }

    /**
     * 第三种形式配置RestTemplateConfig(利用注解LoadBalanced 可在restTemplate直接填应用名实现)
     *
     * @return
     */
    @GetMapping("/getProductList3")
    public String getProductList3() {
        String response = restTemplate.getForObject("http://PRODUCT/product/list", String.class);
        log.info("response={}", response);
        return response;
    }

    /**
     * 用Feign形式调用商品(封装HTTP客户端请求)
     *
     * @return
     */
    @GetMapping("/getProductList4")
    public String getProductList4() {
        String response = productClient.productGetList();
        log.info("response={}", response);
        return response;
    }

    @GetMapping("/listForOder")
    public String listForOder() {
        //读redis
        //业务操作并重新设置redis
        // 异常需要手动回滚redis

        List<ProductInfoOutPut> products = productClient.listForOder();
        log.info("result={}", products);
        return "ok";
    }
}
